package com.atguigu.tingshu.user.service.impl;

import cn.binarywang.wx.miniapp.api.WxMaCodeService;
import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.IdUtil;
import com.atguigu.tingshu.common.constant.RedisConstant;
import com.atguigu.tingshu.model.user.UserInfo;
import com.atguigu.tingshu.user.mapper.UserInfoMapper;
import com.atguigu.tingshu.user.service.UserInfoService;
import com.atguigu.tingshu.vo.user.UserInfoVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.error.WxErrorException;
import org.apache.catalina.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Slf4j
@Service
@SuppressWarnings({"all"})
public class UserInfoServiceImpl extends ServiceImpl<UserInfoMapper, UserInfo> implements UserInfoService {

	@Autowired
	private UserInfoMapper userInfoMapper;

	@Autowired
	private WxMaService wxMaService;

	@Autowired
	private RedisTemplate redisTemplate;

	/**
	 * 根据code去完成微信登录，并把用户信息入库
	 * @param code 用户登录唯一临时凭证
	 * @return
	 */
	/*@Override
	public Map<String, String> wxLogin(String code) {
		try {
			// 调用第三方包去拿到openid
			WxMaJscode2SessionResult sessionInfo = wxMaService.getUserService().getSessionInfo(code);
			if(sessionInfo != null) {
				// 代表有信息, 拿到用户openid唯一标识
				String openid = sessionInfo.getOpenid();
				LambdaQueryWrapper<UserInfo> userInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
				userInfoLambdaQueryWrapper.eq(UserInfo::getWxOpenId, openid);
				// 拿着openid去数据库查，是否存在用户
				UserInfo userInfo = userInfoMapper.selectOne(userInfoLambdaQueryWrapper);
				if(userInfo == null) {
					// 当前用户是首次使用平台，需要把用户信息入库
					userInfo = new UserInfo();
					userInfo.setWxOpenId(openid);
					userInfo.setNickname("听友" + IdUtil.getSnowflake());
					userInfoMapper.insert(userInfo);
					// TODO 需要把用户信息送入kafka，通知账户微服务初始化当前用户的账户信息
				}

				// 生成用户令牌
				String token = IdUtil.fastSimpleUUID();
				// redis存储key
				String loginKey = RedisConstant.USER_LOGIN_KEY_PREFIX + token;
				// redis存值
				UserInfoVo userInfoVo = BeanUtil.copyProperties(userInfo, UserInfoVo.class);
				redisTemplate.opsForValue().set(loginKey, userInfoVo, RedisConstant.USER_LOGIN_KEY_TIMEOUT, TimeUnit.SECONDS);
				Map<String, String> mapResult = new HashMap<String, String>();
				mapResult.put("token", token);
				return mapResult;
			}
			return null;
		} catch (Exception e) {
			log.error("微信登录异常：{}", e);
			throw new RuntimeException(e);
		}
	}*/

	/**
	 * 采用微信JavaSDK进行微信登录
	 * 定死的版本
	 * @param code
	 * @return
	 */
	@Override
	public Map<String, String> wxLogin(String code) {
		try {
			//1.根据code调用微信SDK获取用户会话信息-得到微信用户唯一标识(OpenID)（微信端用户标识不变）-大家无法调用（没有在开发者列表）
			//WxMaJscode2SessionResult sessionInfo = wxMaService.getUserService().getSessionInfo(code);
			//if (sessionInfo != null) {
			//    String openid = sessionInfo.getOpenid();
			String openid = "odo3j4q2KskkbbW-krfE-cAxUnzU";
			//2.根据openId查询用户记录  TODO 固定写死OpenID odo3j4q2KskkbbW-krfE-cAxUnzU
			LambdaQueryWrapper<UserInfo> queryWrapper = new LambdaQueryWrapper<>();
			queryWrapper.eq(UserInfo::getWxOpenId, openid);
			UserInfo userInfo = userInfoMapper.selectOne(queryWrapper);

			//2.1 根据OpenId没有得到用户记录  新增用户记录 且 采用MQ异步初始化账户（余额）信息
			if (userInfo == null) {
				userInfo = new UserInfo();
				userInfo.setWxOpenId(openid);
				userInfo.setNickname("听友" + IdUtil.getSnowflake().nextId());
				userInfo.setAvatarUrl("https://oss.aliyuncs.com/aliyun_id_photo_bucket/default_handsome.jpg");
				userInfoMapper.insert(userInfo);
				//TODO 发送异步MQ消息，通知账户微服务初始化当前用户账户余额信息
				// kafkaService.sendMessage(KafkaConstant.QUEUE_USER_REGISTER, userInfo.getId().toString());
			}
			//2.2 根据OpenID获取到用户记录，

			//3.为登录微信用户生成令牌，将令牌存入Redis中
			String token = IdUtil.fastSimpleUUID();
			String loginKey = RedisConstant.USER_LOGIN_KEY_PREFIX + token;
			UserInfoVo userInfoVo = BeanUtil.copyProperties(userInfo, UserInfoVo.class);
			redisTemplate.opsForValue().set(loginKey, userInfoVo, RedisConstant.USER_LOGIN_KEY_TIMEOUT, TimeUnit.SECONDS);


			//4.响应令牌
			Map<String, String> mapResult = new HashMap<>();
			mapResult.put("token", token);
			return mapResult;
		} catch (Exception e) {
			log.error("微信登录异常：{}", e);
			throw new RuntimeException(e);
		}
	}
}
